package media

import (
	"beesoft.ink/application/media/app/model/media"
	"context"
	"errors"
	"fmt"
)

var Image image

type image struct{}

func (r *image) ImageGeneration(ctx context.Context, req *media.ImageGenerationRequest) (*media.ImageGenerationResponse, error) {
	response := new(media.ImageGenerationResponse)

	client := openai.NewClient(Org.Openai.Apikey)
	result, err := client.CreateImage(ctx, openai.ImageRequest{
		Prompt:         req.Prompt,
		Size:           openai.CreateImageSize256x256,
		ResponseFormat: openai.CreateImageResponseFormatURL,
		N:              1,
	})
	if err != nil {
		return response, errors.New(fmt.Sprintf("Image creation error: %v\n", err))
	}

	response.Content = result.Data[0].URL

	return response, nil
}
